#include "stdafx.h"
#include "TestDatabaseInfo.h"
#include "DatabaseBase.h"
#include "DatabaseInfo.h"
#include "UeConnectionManager.h"

using namespace PDMS::PDMSDBIF;

CPPUNIT_TEST_SUITE_REGISTRATION( TestDatabaseInfo );


TestDatabaseInfo::TestDatabaseInfo()
{
}


TestDatabaseInfo::~TestDatabaseInfo()
{
}

void TestDatabaseInfo::TestOpen()
{
	DatabaseBase base( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	base.DataSource( _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb") );
	UEConnectionPtr ptr = base.DataSource();
	
	DatabaseInfo* pInfo = base.GetDatabaseInfo();
	ASSERT( pInfo != NULL );

	UESoft::ErrorStatus es = UESoft::eOk;

	es = pInfo->Open( _T("All") );
	ASSERT( es == UESoft::eOk );

	CPPUNIT_ASSERT( pInfo->GetVersion() == 1 );
	CPPUNIT_ASSERT( pInfo->GetWorldID() == 0 );
	CPPUNIT_ASSERT( pInfo->GetDBID() == 1 );
	CPPUNIT_ASSERT( pInfo->GetMaxElementID() == 0 );

	pInfo->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}

void TestDatabaseInfo::TestWrite()
{
	DatabaseBase base( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	base.DataSource( _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb") );
	UEConnectionPtr ptr = base.DataSource();
	
	DatabaseInfo* pInfo = base.GetDatabaseInfo();
	ASSERT( pInfo != NULL );
	
	UESoft::ErrorStatus es = UESoft::eOk;
	
	es = pInfo->Open( _T("All") );
	ASSERT( es == UESoft::eOk );
	
	pInfo->SetMaxElementID( 50 );
	pInfo->Update();

	pInfo->Close();
	pInfo->Open( _T("All") );

	CPPUNIT_ASSERT( pInfo->GetVersion() == 1 );
	CPPUNIT_ASSERT( pInfo->GetWorldID() == 0 );
	CPPUNIT_ASSERT( pInfo->GetDBID() == 1 );
	CPPUNIT_ASSERT( pInfo->GetMaxElementID() == 50 );
	
	pInfo->SetMaxElementID( 0 );
	pInfo->Update();
	pInfo->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}